//
//  DMTiXianViewController.m
//  DMGME
//
//  Created by MC on 15/10/10.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "DMTiXianViewController.h"
#import "DMTiXianRecordViewController.h"
@interface DMTiXianViewController ()<UIAlertViewDelegate>

@end

@implementation DMTiXianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSomeView];
    
    
}
-(void)createSomeView
{
    [self reloadInputViews];
    UIView *leftView = [[UIView alloc] init];
    leftView.frame = CGRectMake(15, KHeight/18, 10, KHeight/20);
    leftView.backgroundColor = ComColor;
    [self.view addSubview:leftView];
    UILabel *yueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftView.frame)+10, Y(leftView), KWidth, HEIGHT(leftView))];
    if ([_yueStr  isEqual: @""]) {
        yueLabel.text = [NSString stringWithFormat:@"账户余额:￥0"];

    }else
    {
        yueLabel.text = [NSString stringWithFormat:@"账户余额:￥%@",_yueStr];
 
    }
    [self.view addSubview:yueLabel];
    
    UITextField *yueText = [[UITextField alloc] initWithFrame:CGRectMake(leftView.frame.origin.x, leftView.frame.origin.y+leftView.frame.size.height+20, leftView.frame.size.width+yueLabel.frame.size.width/2*1, 34)];
    [yueText setBorderStyle:UITextBorderStyleRoundedRect];
    yueText.keyboardType = UIKeyboardTypeNumberPad;
    yueText.tag = 120;
    
    [self.view addSubview:yueText];
    UILabel *RMB = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(yueText.frame)+10, yueText.frame.origin.y, 30, yueText.frame.size.height)];
    RMB.text = @"(元)";
    RMB.textColor = [UIColor grayColor];
    [self.view addSubview:RMB];
    UILabel *tishiLab = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(RMB.frame)+10, KWidth-10, 21)];
    tishiLab.textColor = [UIColor grayColor];
    tishiLab.font = [UIFont systemFontOfSize:15];
    tishiLab.text = @"余额大于1元时才能提现,每天最多可提现10次。";
    [self.view addSubview:tishiLab];
    UIButton *tixianBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    tixianBtn.frame = CGRectMake(15, CGRectGetMaxY(tishiLab.frame)+30, 100, 40);
    tixianBtn.backgroundColor = ComColor;
    [tixianBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tixianBtn setTitle:@"立即提现" forState:UIControlStateNormal];
    [tixianBtn addTarget:self action:@selector(tixianBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tixianBtn];
}
-(void)tixianBtnClick:(UIButton *)sender
{
    UITextField *yueText = (UITextField *)[self.view viewWithTag:120];
    [yueText resignFirstResponder];
    int i = [yueText.text intValue];

    if (i>[_yueStr intValue] ||i<1) {
        UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的输入金额超出提现区间" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alt show];
  

    }else
    {
        
        NSMutableDictionary *weiChatDic = [NSMutableDictionary dictionary];
        [weiChatDic setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$aplay"] forKey:@"token"];
        [weiChatDic setValue:kkUserDict[@"id"] forKey:@"agent_id"];
        [weiChatDic setValue:yueText.text forKey:@"money"];
        [weiChatDic setValue:@"aplay" forKey:@"action"];
        [self StartLoading];
        [AFDownManager POST:BindingWeiChat parameters:weiChatDic success:^(id responseObject) {
//            NSLog(@"申请提现 ==%@",responseObject);
            [self StopLoading];
            if([responseObject[@"code"] integerValue]  == 0)
            {
                UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"成功" message:@"产看提现记录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                alt.tag = 125;
                [alt show];
                NSDictionary *notiDic = [[NSDictionary alloc] initWithObjectsAndKeys:yueText.text,@"yuE",nil];
                //创建通知
                NSNotification *notification =[NSNotification notificationWithName:@"yuE" object:nil userInfo:notiDic];
                //通过通知中心发送通知
                [[NSNotificationCenter defaultCenter] postNotification:notification];
                
            }
           else
           {
               UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"提示" message:responseObject[@"desc"] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
               [alt show];
           }

        } failure:^(NSError *error) {
            [self StopLoading];
            [self showMsg:@"提现失败"];
//            NSLog(@"dataRequest error = %@",error);
            
        }];

    }
    }
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 125 && buttonIndex == 1) {
        DMTiXianRecordViewController *record = [[DMTiXianRecordViewController alloc] init];
        [self.navigationController pushViewController:record animated:YES];
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
